<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class AdvContent extends Model
{
    use SoftDeletes;
    protected $table = 'adv_contents';

    /**
     * 定义主键
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 禁止批量赋值的字段
     *
     * @var array
     */
    protected $guarded = ['id','created_at','deleted_at'];

    /**
     * 软删除应该被调整为日期的属性
     * [$dates description]
     * @var [type]
     */
    protected $dates=[
        'created_at', 'updated_at','deleted_at'
    ];

    // 广告表 关联 类型表
    public function adv_type_info(){
        return $this -> hasOne(AdvType::class,'id','adv_type_id');
    }

    public function region(){
        return $this -> hasOne(SystemRegion::class,'id','region_id');
    }

    public function article(){
        return $this -> hasOne(Article::class,'id','obj_id');
    }


    protected $appends = ['adv_img_url', 'adv_img_pc_url'];

    public function getAdvImgUrlAttribute()
    {
        if ($this->adv_img) {
            return config('app.img_url') . $this->adv_img;
        } else {
            return false;
        }
    }


    public function getAdvImgPcUrlAttribute()
    {
        if ($this->adv_img_pc) {
            return config('app.img_url') . $this->adv_img_pc;
        } else {
            return false;
        }
    }



    /**
     * 广告分类
     * @param int $advTypeId
     * @param int $pagesize
     * @param int $regionId
     * @param int $advContentId
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public static function getAdvContent($advTypeId = 0, $pagesize = 0, $regionId = 0, $advContentId = 0)
    {
        $query = self::query();
        $query->where('is_show', 1);
        if ($advTypeId) {
            $query->where('adv_type_id', $advTypeId);
        }

        $query->where(function ($query_region) use ($regionId) {
            $query_region->where('region_id', 0);
            $query_region->orWhere('region_id', null);
            if ($regionId > 0) {
                $query_region->orWhere('region_id', $regionId);
            }
        });

        if ($advContentId) {
            $query->where('id', $advContentId);
        }

        if ($pagesize) {
            $query->limit($pagesize);
        }
        $query->orderBy('sort', 'asc');
        $advContent = $query->get(['id', 'adv_name', 'adv_img', 'adv_img_pc', 'adv_type', 'obj_id', 'url']);


        if (!$advContent->isEmpty()) {

            foreach ($advContent as &$advContentVal) {
                $advContentVal->pc_url = ''; // pc端页面跳转地址
                $objData = [
                    'id' => 0,
                    'title' => '',
                    'img' => '',
                ];
                // 类型（1文章；2调查问卷；3地址;4商品）
                if ($advContentVal->adv_type == 1) {
                    $article = Article::find($advContentVal->obj_id);
                    if ($article) {
                        $objData['id'] = $article->id;
                        $objData['title'] = $article->article_title;
                        $objData['img'] = $article->list_img_url;
                        $objData['zan_num'] = $article->zan_num;
                        $objData['read_num'] = $article->read_num;
                        $objData['content_into'] = $article->content_into;
                        $advContentVal->pc_url = url('article/details/'.$article->id);
                    }
                } else if ($advContentVal->adv_type == 4) {
                    $goods = AgricGoods::find($advContentVal->obj_id);
                    if ($goods) {
                        $objData['id'] = $goods->id;
                        $objData['title'] = $goods->goods_name;
                        $objData['img'] = $goods->goods_img_url;
                        $advContentVal->pc_url = url('shop/details/'.$goods->id);
                    }
                }
                $advContentVal->obj_data = $objData;
            }
        }

        return $advContent;

    }

}
